#include<iostream>
#include<unistd.h>

#include<string>
#include<fstream>
#include<jsoncpp/json/json.h>

struct stu
{
    std::string name;
    int age;
    double weight;
    public:
    void debug()
    {
    std::cout<<name<<std::endl;
    std::cout<<age<<std::endl;
    std::cout<<weight<<std::endl;
    }
};

int main()
{
   //3.从文件里反序列化读出来
    std::ifstream in("out.txt");
    if(!in.is_open())
    {
        return 1;
    }
    char buffer[1024];
    in.read(buffer,sizeof(buffer));//读取
    in.close();

    std::string json_string = buffer;
    //反序列化
    Json::Value root;
    Json::Reader reader;//读到系统中反序列化
    bool res = reader.parse(json_string,root);
    (void)res;
    struct stu zs;
    zs.name = root["name"].asCString();
    zs.age = root["age"].asInt();
    zs.weight=root["weight"].asDouble();
    zs.debug();

    //1.结构化 序列化
    // struct stu sz = {"小明",18,60};
    // //转换成为字符串
    // Json::Value root;//创建Json对象
    // root["name"] = sz.name;
    // root["age"] = sz.age;
    // root["weight"] = sz.weight;
    // //嵌套
    //root["self"] = root;
   // 1.Json::FastWriter writer;
    //2 Json::StyledWriter writer;
    // Json::StyledWriter writer;
    // std::string str = writer.write(root);
    // //写到文件
    // std::ofstream out("out.txt");
    // if(!out.is_open())
    // {
    //     std::cout<<str;
    //     return 1;
    // }
    // out<<str;
    // out.close();

    return 0;
}